{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# 9. Exporting Data Example\n",
    "\n",
    "This notebook shows different ways to export data from memory. This first example exports all conversations from local SQLite memory with their respective score values in a JSON format. The data can currently be exported both as JSON file or a CSV file that will be saved in your results folder within PyRIT. The CSV export is commented out below. In this example, all conversations are exported, but by using other export functions from `memory_interface`, we can export by specific labels and other methods."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "eab90a77-9d7e-450a-8137-1aa54e9d9af2\n",
      "{}: user: Hi, chat bot! This is my initial prompt.\n",
      "{}: assistant: Nice to meet you! This is my response.\n",
      "{}: user: Wonderful! This is my second prompt to the chat bot!\n",
      "Exported conversation with scores to JSON: C:\\git\\PyRIT\\dbdata\\conversation_and_scores_json_example.json\n"
     ]
    }
   ],
   "source": [
    "from uuid import uuid4\n",
    "\n",
    "from pyrit.common.path import DB_DATA_PATH\n",
    "from pyrit.memory import CentralMemory\n",
    "from pyrit.models import Message, MessagePiece\n",
    "from pyrit.setup import IN_MEMORY, initialize_pyrit\n",
    "\n",
    "initialize_pyrit(memory_db_type=IN_MEMORY)\n",
    "\n",
    "conversation_id = str(uuid4())\n",
    "\n",
    "print(conversation_id)\n",
    "\n",
    "message_list = [\n",
    "    MessagePiece(\n",
    "        role=\"user\", original_value=\"Hi, chat bot! This is my initial prompt.\", conversation_id=conversation_id\n",
    "    ),\n",
    "    MessagePiece(\n",
    "        role=\"assistant\", original_value=\"Nice to meet you! This is my response.\", conversation_id=conversation_id\n",
    "    ),\n",
    "    MessagePiece(\n",
    "        role=\"user\",\n",
    "        original_value=\"Wonderful! This is my second prompt to the chat bot!\",\n",
    "        conversation_id=conversation_id,\n",
    "    ),\n",
    "]\n",
    "\n",
    "sqlite_memory = CentralMemory.get_memory_instance()\n",
    "sqlite_memory.add_message_to_memory(request=Message([message_list[0]]))\n",
    "sqlite_memory.add_message_to_memory(request=Message([message_list[1]]))\n",
    "sqlite_memory.add_message_to_memory(request=Message([message_list[2]]))\n",
    "\n",
    "entries = sqlite_memory.get_conversation(conversation_id=conversation_id)\n",
    "\n",
    "for entry in entries:\n",
    "    print(entry)\n",
    "\n",
    "# Define file path for export\n",
    "json_file_path = DB_DATA_PATH / \"conversation_and_scores_json_example.json\"\n",
    "# csv_file_path = DB_DATA_PATH / \"conversation_and_scores_csv_example.csv\"\n",
    "\n",
    "# Export the data to a JSON file\n",
    "conversation_with_scores = sqlite_memory.export_conversations(file_path=json_file_path, export_type=\"json\")\n",
    "print(f\"Exported conversation with scores to JSON: {json_file_path}\")\n",
    "\n",
    "# Export the data to a CSV file\n",
    "# conversation_with_scores = sqlite_memory.export_conversations(file_path=csv_file_path, export_type=\"csv\")\n",
    "# print(f\"Exported conversation with scores to CSV: {csv_file_path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2",
   "metadata": {},
   "source": [
    "You can also use the exported JSON or CSV files to import the data as a NumPy DataFrame. This can be useful for various data manipulation and analysis tasks."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>role</th>\n",
       "      <th>conversation_id</th>\n",
       "      <th>sequence</th>\n",
       "      <th>timestamp</th>\n",
       "      <th>labels</th>\n",
       "      <th>targeted_harm_categories</th>\n",
       "      <th>prompt_metadata</th>\n",
       "      <th>converter_identifiers</th>\n",
       "      <th>prompt_target_identifier</th>\n",
       "      <th>...</th>\n",
       "      <th>original_value_data_type</th>\n",
       "      <th>original_value</th>\n",
       "      <th>original_value_sha256</th>\n",
       "      <th>converted_value_data_type</th>\n",
       "      <th>converted_value</th>\n",
       "      <th>converted_value_sha256</th>\n",
       "      <th>response_error</th>\n",
       "      <th>originator</th>\n",
       "      <th>original_prompt_id</th>\n",
       "      <th>scores</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2c8af709-aa06-41d1-80e8-38bd510912e6</td>\n",
       "      <td>user</td>\n",
       "      <td>eab90a77-9d7e-450a-8137-1aa54e9d9af2</td>\n",
       "      <td>0</td>\n",
       "      <td>2025-10-23 16:31:40.648952</td>\n",
       "      <td>{}</td>\n",
       "      <td>NaN</td>\n",
       "      <td>{}</td>\n",
       "      <td>[]</td>\n",
       "      <td>{}</td>\n",
       "      <td>...</td>\n",
       "      <td>text</td>\n",
       "      <td>Hi, chat bot! This is my initial prompt.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>text</td>\n",
       "      <td>Hi, chat bot! This is my initial prompt.</td>\n",
       "      <td>NaN</td>\n",
       "      <td>none</td>\n",
       "      <td>undefined</td>\n",
       "      <td>2c8af709-aa06-41d1-80e8-38bd510912e6</td>\n",
       "      <td>[]</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                     id  role  \\\n",
       "0  2c8af709-aa06-41d1-80e8-38bd510912e6  user   \n",
       "\n",
       "                        conversation_id  sequence                  timestamp  \\\n",
       "0  eab90a77-9d7e-450a-8137-1aa54e9d9af2         0 2025-10-23 16:31:40.648952   \n",
       "\n",
       "  labels  targeted_harm_categories prompt_metadata converter_identifiers  \\\n",
       "0     {}                       NaN              {}                    []   \n",
       "\n",
       "  prompt_target_identifier  ... original_value_data_type  \\\n",
       "0                       {}  ...                     text   \n",
       "\n",
       "                             original_value original_value_sha256  \\\n",
       "0  Hi, chat bot! This is my initial prompt.                   NaN   \n",
       "\n",
       "  converted_value_data_type                           converted_value  \\\n",
       "0                      text  Hi, chat bot! This is my initial prompt.   \n",
       "\n",
       "  converted_value_sha256 response_error  originator  \\\n",
       "0                    NaN           none   undefined   \n",
       "\n",
       "                     original_prompt_id scores  \n",
       "0  2c8af709-aa06-41d1-80e8-38bd510912e6     []  \n",
       "\n",
       "[1 rows x 22 columns]"
      ]
     },
     "execution_count": null,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd  # type: ignore\n",
    "\n",
    "df = pd.read_json(json_file_path)\n",
    "df.head(1)"
   ]
  }
 ],
 "metadata": {
  "jupytext": {
   "main_language": "python"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
